package servlet;

import hibernate.RequisitionInfo;

import java.io.IOException;
import java.io.PrintWriter;
import java.text.SimpleDateFormat;
import java.util.List;
import java.util.Vector;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.et.mvc.JsonView;

import DAO.RequisitionInfoDAO;

public class SearchGZSQ extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    public SearchGZSQ() 
    {
        super();
    }

	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException 
	{
	    request.setCharacterEncoding("utf-8");
		response.setCharacterEncoding("utf-8");
		response.setContentType("text/plain");
		PrintWriter out = response.getWriter(); 
		
		String start = request.getParameter("start");
		String limit = request.getParameter("limit");
	    String department = request.getParameter("department");
	    String proposer = request.getParameter("proposer");
	    String time1 = request.getParameter("time1");
	    String time2 = request.getParameter("time2");
	    String deviceName = request.getParameter("deviceName");
	    String produceUnit = request.getParameter("produceUnit");
	    	    
	    RequisitionInfoDAO requisition = new RequisitionInfoDAO();
	    @SuppressWarnings("unchecked")
	    Vector vt = new Vector();
	    
	    if(("".equals(department))&&("".equals(proposer))&&("".equals(time1))
	        &&("".equals(time2))&&("".equals(deviceName))&&("".equals(produceUnit)))
	    {
	    	vt = requisition.getAll(start,limit);   	
	    }
	    else
	    {
	    	vt = requisition.searchRequisitionInfo(start,limit,department, proposer, time1, time2, deviceName, produceUnit);
	    }
	    
	    String count = (vt.elementAt(0)).toString();
	    @SuppressWarnings("unchecked")
	    List list = (List)vt.elementAt(1);
	   for(int i=0;i<list.size();i++)
	    {
			//时间格式转换
	        SimpleDateFormat  dateFormat = new SimpleDateFormat("yyyy-MM-dd");	        
	        @SuppressWarnings("unused")
			String time = dateFormat.format(((RequisitionInfo)list.get(i)).getTime());
	        //SimpleDate sd = new SimpleDate(((RequisitionInfo)list.get(i)).getTime());
	    }
	    JsonView jv = new JsonView(list);
	    @SuppressWarnings("unused")
		String sss = "{totalCount:" + count + "," + "result:" +jv.toString()+"}";
	    
	    
		String s = "{totalCount:" + count + "," + "result:[";
		for(int i=0;i<list.size();i++)
		{			
			s = s + "{id:" + "'" + ((RequisitionInfo)list.get(i)).getRid() + "',";
			s = s + "department:" + "'" + ((RequisitionInfo)list.get(i)).getDepartment() + "',";
			s = s + "proposer:" + "'" + ((RequisitionInfo)list.get(i)).getProposer() + "',";
			
			//时间格式转换
	        SimpleDateFormat  dateFormat = new SimpleDateFormat("yyyy-MM-dd");	        
	        String time = dateFormat.format(((RequisitionInfo)list.get(i)).getTime());
	        s = s + "time:" + "'" + time + "',";
			
			s = s + "deviceName:" + "'" + ((RequisitionInfo)list.get(i)).getDeviceName() + "',";
			s = s + "model:" + "'" + ((RequisitionInfo)list.get(i)).getModel() + "',";
			s = s + "produceUnit:" + "'" + ((RequisitionInfo)list.get(i)).getProduceUnit() + "',";
			s = s + "quantity:" + "'" + ((RequisitionInfo)list.get(i)).getQuantity() + "',";
			s = s + "money:" + "'" + ((RequisitionInfo)list.get(i)).getMoney() + "',";
			s = s + "reason:" + "'" + ((RequisitionInfo)list.get(i)).getReason() + "',";
			s = s + "purchaseCondition:" + "'" + ((RequisitionInfo)list.get(i)).getPurchaseCondition() + "',";
			s = s + "personCondition:" + "'" + ((RequisitionInfo)list.get(i)).getPersonCondition() + "',";
			s = s + "economyBenefit:" + "'" + ((RequisitionInfo)list.get(i)).getEconomyBenefit() + "',";
			s = s + "useRate:" + "'" + ((RequisitionInfo)list.get(i)).getUseRate() + "',";
			s = s + "nowStatus:" + "'" + ((RequisitionInfo)list.get(i)).getNowStatus() + "',";
			s = s + "remark:" + "'" + ((RequisitionInfo)list.get(i)).getRemark() + "',";
			s = s + "approveOpinion:" + "'" + ((RequisitionInfo)list.get(i)).getApproveOpinion() + "',";	
			s = s + "approveRemark:" + "'" + ((RequisitionInfo)list.get(i)).getApproveRemark() + "'},";
		}
		String ss = "{totalCount:" + list.size() + "," + "result:[";
        if(!ss.equals(s))
        {
        	s = s.substring(0, s.length()-1);
        }
        
        s = s + "]}";
        out.println(s);
        out.flush();
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException 
	{
		doGet(request, response);
	}

}
